﻿@using Fluxor.Blazor.Web.Components;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore;
@using SimpleIdServer.IdServer.Website.Stores.ScopeStore;
@inherits FluxorComponent
@inject Radzen.DialogService dialogService
@inject NotificationService notificationService
@inject IState<AddScopeState> addScopeState
@inject IDispatcher dispatcher

<RadzenSteps>
    <Steps>
        <RadzenStepsItem Text="@Global.MapperType">
            <div class="row gy-4">
                @foreach (var mapperType in MapperTypes.Where(m => (Scope.Protocol == ScopeProtocols.OPENID) || (Scope.Protocol == ScopeProtocols.SAML && m.IsSAMLSupported)))
                {
                    <div class="col-md-6">
                        <RadzenCard @onclick="() => SelectMapper(mapperType)" class="@(SelectedMapperType != null && SelectedMapperType.Name == mapperType.Name ? "selected selectable" : "selectable")">
                            <div class="row">
                                <div class="col-md-3">
                                    <RadzenImage Path="@mapperType.PictureUrl" Style="width: 60px" />
                                </div>
                                <div class="col">
                                    <h5>@mapperType.Name</h5>
                                    <p class="text-muted">@(new MarkupString(mapperType.ShortDescription))</p>
                                </div>
                            </div>
                        </RadzenCard>
                    </div>
                }
            </div>

            <hr class="hr" />

            @if (SelectedMapperType != null)
            {
                <h5><RadzenIcon Icon="info" /> @SelectedMapperType.Name</h5>
                <p class="text-muted">@(new MarkupString(SelectedMapperType.Description))</p>
            }
        </RadzenStepsItem>
        <RadzenStepsItem Text="@Global.Create" Disabled="@(SelectedMapperType == null)">
            @switch(SelectedMapperType.Type)
            {
                case MappingRuleTypes.USERATTRIBUTE:
                    <UserAttributeScopeMapper ScopeMapperSaved="AddScopeMapper" Scope="@Scope" IsEditModeEnabled="false"></UserAttributeScopeMapper>
                    break;
                case MappingRuleTypes.USERPROPERTY:
                    <UserPropertyScopeMapper ScopeMapperSaved="AddScopeMapper" Scope="@Scope" IsEditModeEnabled="false"></UserPropertyScopeMapper>
                    break;
            }
        </RadzenStepsItem>
    </Steps>
</RadzenSteps>

@code {
    [Parameter]
    public Scope Scope { get; set; } = null!;

    record MapperType
    {
        public string Name { get; set; } = null!;
        public string ShortDescription { get; set; } = null!;
        public string Description { get; set; } = null!;
        public MappingRuleTypes Type { get; set; }
        public string PictureUrl { get; set; } = null!;
        public bool IsSAMLSupported { get; set; } = true;
    }

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if (firstRender)
        {
            SubscribeToAction<AddScopeClaimMapperSuccessAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.ScopeClaimMapperAdded });
                dialogService.Close();
                StateHasChanged();
            });
            dispatcher.Dispatch(new StartUpdateScopeMapperAction());
    }
    }

    ICollection<MapperType> MapperTypes { get; set; } = new List<MapperType>
    {
        new MapperType { Name = Global.Attribute, ShortDescription = Global.UserAttribute, Description = Global.MapUserAttributeToClaim, Type = MappingRuleTypes.USERATTRIBUTE, PictureUrl = "_content/SimpleIdServer.IdServer.Website/images/UserAttribute.png" },
        new MapperType { Name = Global.Property, ShortDescription = Global.UserProperty, Description = Global.MapUserPropertyToClaim, Type = MappingRuleTypes.USERPROPERTY, PictureUrl = "_content/SimpleIdServer.IdServer.Website/images/UserProperty.png" }
    };

    MapperType? SelectedMapperType { get; set; } = null;

    void SelectMapper(MapperType mapperType)
    {
        SelectedMapperType = mapperType;
    }

    void AddScopeMapper(ScopeClaimMapper claimMapper)
    {
        claimMapper.Id = Guid.NewGuid().ToString();
        dispatcher.Dispatch(new AddScopeClaimMapperAction { ClaimMapper = claimMapper, ScopeId = Scope.Id });
    }
}